package com.fastloan.mgt.invest.service.impl;


import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.invest.dao.InvestSubjectMapper;
import com.fastloan.mgt.invest.dao.InvestTempSubjectMapper;
import com.fastloan.mgt.invest.dao.RegionSubjectMapper;
import com.fastloan.mgt.invest.model.InvestSubject;
import com.fastloan.mgt.invest.model.InvestTempSubject;
import com.fastloan.mgt.invest.model.RegionSubject;
import com.fastloan.mgt.invest.model.vo.InvestSubjectCusVo;
import com.fastloan.mgt.invest.model.vo.InvestSubjectVo;
import com.fastloan.mgt.invest.service.InvestSubjectService;
import com.fastloan.safe.core.param.RoleTypeEnum;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:32
 */
@Service
public class InvestSubjectServiceImpl extends BasicServiceImpl<InvestSubject> implements InvestSubjectService {

    @Autowired
    private InvestSubjectMapper investSubjectMapper;

    @Autowired
    private InvestTempSubjectMapper investTempSubjectMapper;

    @Autowired
    private RegionSubjectMapper regionSubjectMapper;

    @Override
    @DataTable(name = "investSubjectPageList")
    public List<InvestSubjectVo> investSubjectPageList(Map<String, Object> map) {
        map.put("orgTopId", ManagerKit.getTopOrgId());
        if (ManagerKit.getRole().getIndexType() == RoleTypeEnum.MANAGER.getCode()) {
            map.put("managerId", ManagerKit.getUserId());
        }
        return investSubjectMapper.investSubjectPageList(map);
    }

    @Override
    @DataTable(name = "forCustomerPageList")
    public List<InvestSubjectCusVo> forCustomerPageList(Map<String, Object> map) {
        if (ManagerKit.getRole().getIndexType() == RoleTypeEnum.MANAGER.getCode()) {
            map.put("managerId", ManagerKit.getUserId());
        }
        return investSubjectMapper.forCustomerPageList(map);
    }

    @Override
    @Transactional
    public void insertForTempIds(InvestSubject investSubject, String tempIds, String regionIds) {
        insertSelective(investSubject);
        String[] splitTemp = tempIds.split(",");
        List<InvestTempSubject> investTempSubjects = new ArrayList<>();
        for (int i = 0; i < splitTemp.length; i++) {
            InvestTempSubject investTempSubject = new InvestTempSubject();
            investTempSubject.setSubjectId(investSubject.getId());
            investTempSubject.setTempId(splitTemp[i]);
            investTempSubjects.add(investTempSubject);
        }
        investTempSubjectMapper.insertList(investTempSubjects);

        String[] splitRegion = regionIds.split(",");
        List<RegionSubject> regionSubjects = new ArrayList<>();
        for (int i = 0; i < splitRegion.length; i++) {
            RegionSubject regionSubject = new RegionSubject();
            regionSubject.setSubjectId(investSubject.getId());
            regionSubject.setRegionId(splitRegion[i]);
            regionSubjects.add(regionSubject);
        }
        regionSubjectMapper.insertList(regionSubjects);
    }

    @Override
    public List<InvestSubject> selectSubjectByManagerId() {
        String orgId = ManagerKit.getOrgId();
        InvestSubject investSubject = new InvestSubject();
        investSubject.setOrgId(orgId);
        investSubject.setState(1);
        return investSubjectMapper.select(investSubject);
    }

    @Override
    @Transactional
    public void update(InvestSubject investSubject, String regionIds) {
        if (regionIds != null) {
            String[] splitRegion = regionIds.split(",");
            RegionSubject regionSubjectOld = new RegionSubject();
            regionSubjectOld.setSubjectId(investSubject.getId());
            regionSubjectMapper.delete(regionSubjectOld);
            List<RegionSubject> regionSubjects = new ArrayList<>();
            for (int i = 0; i < splitRegion.length; i++) {
                RegionSubject regionSubject = new RegionSubject();
                regionSubject.setSubjectId(investSubject.getId());
                regionSubject.setRegionId(splitRegion[i]);
                regionSubjects.add(regionSubject);
            }
            regionSubjectMapper.insertList(regionSubjects);
        }
        updateByPrimaryKeySelective(investSubject);
    }

    @Override
    public List<InvestSubject> haveName(String name, String topOrgId) {
        return investSubjectMapper.haveName(name, topOrgId);
    }
}
